package net.looker.monitor.parser;

import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import net.looker.data.Media;
import net.looker.data.News;
import net.looker.util.Html;
import net.looker.util.URLUtils;

/**
 * 链接解析器
 * @author solosky
 *
 */


public class LinkParser extends Parser
{
	/**
	 * 构造函数
	 * @param news
	 */
	public LinkParser(News news)
	{
		super(news);
	}

	@Override
	public boolean parse()
	{
		//解析出连接
		Logger logger = Logger.getLogger("looker");
		String regxp = "<a.*?href=['\"](.*?)['\"].*?>(.*?)</a>";
		boolean matched = false;
		Media media = null;
		Pattern pt = Pattern.compile(regxp,Pattern.CASE_INSENSITIVE);	//不区分大小写
		Matcher mc = pt.matcher(news.getContent());
		while (mc.find())
		{
			logger.info("成功解析链接:"+mc.group(1));
			media = new Media();
			media.setUrl(URLUtils.real(mc.group(1), news.getUrl()));
			media.setOriginal(mc.group(1));
			media.setTitle(Html.stripTags(mc.group(2)));
			
			addMedia(media);

			matched = true;
		}
		return matched;
	}
}
